#!/bin/bash

# 获取脚本文件的完整路径
SCRIPT_FULL_PATH=$(realpath "$0")
# 获取脚本文件所在的目录
SCRIPT_DIR=$(dirname "$SCRIPT_FULL_PATH")
# 获取当前日期和时间
# 获取当前日期
CURRENT_DATE=$(date +%Y-%m-%d)
CURRENT_TIME=$(date +"%H%M%S")
sqlbackdir="${SCRIPT_DIR}/data"
DB_NAME="yuyue_api"

cd "$sqlbackdir"

backsqlName=()
backsqlName[0]="${sqlbackdir}/yuyue_api_${CURRENT_DATE}_${CURRENT_TIME}.sql"
#backsqlName[1]="${sqlbackdir}/beishang_erp.sql"

for sql_name in ${backsqlName[@]}; do
    #文件存在则删除
    if [ -e "$sql_name" ]; then
      rm -rf  "$sql_name"
    fi
    #yuyue_api_2024-11-19_11-55
    table_name=$(basename ${sql_name} .sql)


    echo "$sql_name";
    mysqldump  --single-transaction -uroot -plamp7918I  "$DB_NAME" > "$sql_name"

    # sql.gz
    gzip $sql_name
#    sshpass -p lamp7918I scp -P19748 -l 86000 "$sql_name"  root@216.00.153.234:"$dist_dir"
done

echo "${DB_NAME}_${CURRENT_DATE}*";
#删除非今天的备份文件
find "$sqlbackdir" -type f -name "*.gz" ! -name "${DB_NAME}_${CURRENT_DATE}*" -exec rm {} \;

echo "Old backups cleaned up"

